# -*- coding: utf-8 -*-


from bs4 import BeautifulSoup
import csv

html = """
<table class="tablelist" cellpadding="0" cellspacing="0">
    <tbody>
        <tr class="h">
            <td class="l" width="374">职位名称</td>
            <td>职位类别</td>
            <td>人数</td>
            <td>地点</td>
            <td>发布时间</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=33824&keywords=python&tid=87&lid=2218">22989-金融云区块链高级研发工程师（深圳）</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=29938&keywords=python&tid=87&lid=2218">22989-金融云高级后台开发</a></td>
            <td>技术类</td>
            <td>2</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=31236&keywords=python&tid=87&lid=2218">SNG16-腾讯音乐运营开发工程师（深圳）</a></td>
            <td>技术类</td>
            <td>2</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=31235&keywords=python&tid=87&lid=2218">SNG16-腾讯音乐业务运维工程师（深圳）</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-25</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=34531&keywords=python&tid=87&lid=2218">TEG03-高级研发工程师（深圳）</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=34532&keywords=python&tid=87&lid=2218">TEG03-高级图像算法研发工程师（深圳）</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=31648&keywords=python&tid=87&lid=2218">TEG11-高级AI开发工程师（深圳）</a></td>
            <td>技术类</td>
            <td>4</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a target="_blank" href="position_detail.php?id=32218&keywords=python&tid=87&lid=2218">15851-后台开发工程师</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="even">
            <td class="l square"><a target="_blank" href="position_detail.php?id=32217&keywords=python&tid=87&lid=2218">15851-后台开发工程师</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
        <tr class="odd">
            <td class="l square"><a id="test" class="test" target='_blank' href="position_detail.php?id=34511&keywords=python&tid=87&lid=2218">SNG11-高级业务运维工程师（深圳）</a></td>
            <td>技术类</td>
            <td>1</td>
            <td>深圳</td>
            <td>2017-11-24</td>
        </tr>
    </tbody>
</table>
"""
soup = BeautifulSoup(html, 'lxml')
# 找所有的a标签与p标签
# print(soup.find_all(['a', 'p']))
# print(soup.find_all(['title', 'p']))
# # print(soup.find_all('p'))

# 获取tr标签 class="even" (找多个， find_all)
# trs = soup.find_all('tr')  # 错的
# trs = soup.find_all('tr', class='even') # 错的
# trs = soup.find_all('tr', class_='even')
# trs = soup.find_all('tr', attrs={'class':'even'})
# for tr in trs:
#     print(tr)


# 通过多个属性定位 <a id="test" class="test" target='_blank
# lst = soup.find_all('a', id="test", class_="test")
# for a in lst:
#     print(a)


#  需求: 获取所有a标签href属性值
# a_lst = soup.find_all('a')
# for a in a_lst:
#     # print(a.attrs['href'])
#     # print(a['href'])
#     print(a.get('href'))

# 需求: 爬取所有的职位名称  先找tr标签  find_all(获取多个tr标签)
trs = soup.find_all('tr')[1:]
lst = []
for tr in trs:
    dic = {}
    tds = tr.find_all('td')  # 返回的数据类型: list
    dic['job_name'] = tds[0].string
    dic['addr'] = tds[-2].string
    # print(job_name, addr)
    # jsob_name = tds.strings
    # print(jsob_name)
    lst.append(dic)
with open('aaa.csv', 'w', encoding='utf-8-sig', newline='')as f:
    writer = csv.DictWriter(f, fieldnames=('job_name', 'addr'))
    writer.writeheader()
    writer.writerows(lst)